package com.lagou.comment.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.lagou.comment.CommentService;
import com.lagou.entity.CourseComment;
import mapper.CourseCommentDao;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service//暴露服务
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CourseCommentDao commentDao;

    //保存留言
    @Override
    public Integer saveComment(CourseComment courseComment) {
        Integer i = commentDao.saveComment (courseComment);
        return i;
    }

    //查询留言
    @Override
    public List<CourseComment> getCommentByCourseId(Integer courseid, Integer offset, Integer pageSize) {
        return commentDao.getCommentByCourseId (courseid,offset,pageSize);
    }



    @Override
    public Integer saveFavorite(Integer comment_id, Integer userid) {
        //查询当前用户留言是否点过赞
        Integer i = commentDao.existsFavorite (comment_id, userid);
        int i1 = 0;
        int i2 = 0;
        if (i == 0) {
            i1 = commentDao.saveCommentFavorite (comment_id, userid);
        } else {
            i1 = commentDao.updateFavoriteStatus (0,comment_id,userid);
        }
        //更新赞的数量
        i2 = commentDao.updateLikeCount (1,comment_id);
        if (i1==0 || i2 ==0) {
            throw new RuntimeException ("点赞失败");
        }
        return comment_id;
    }

    /**
     * 删除点赞的信息（is_del = 1）
     * 更新留言赞的数量-1
     * @param comment_id 留言编号
     * @param userid 用户编号
     * @return 0:失败，1：成功
     */
    @Override
    public Integer cancelFavorite(Integer comment_id, Integer userid) {
        Integer i1 = commentDao.updateFavoriteStatus (1, comment_id, userid);
        Integer i2 = commentDao.updateLikeCount (-1, comment_id);
        if (i1==0 || i2 ==0) {
            throw new RuntimeException ("取消赞失败");
        }
        return i2;
    }


}
